package com.lanyun.controller;


import com.lanyun.pojo.Warehouse;
import com.lanyun.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 李向南
 * 仓库
 */
@Controller
public class WarehouseController {

    @Autowired
     private WarehouseService wareservice;


     Warehouse warehouse=new Warehouse();

    //查询所有仓库
    @RequestMapping("/warehouse")
    public String showAllWare(Map<String,Object> map){
        List<Warehouse> list=wareservice.listAllw();
        List adminName=wareservice.listAllAdminName();
        map.put("allWare",list);
        map.put("listAllAdminName",adminName);
        return "warehouse";
    }

    //增加仓库
    @RequestMapping(value="/addw",method= RequestMethod.POST)
    @ResponseBody
    public int addWarehouse(HttpServletRequest request, HttpServletResponse response){
        String name=request.getParameter("w_name");
        String site=request.getParameter("w_site");
        String w_admin=request.getParameter("w_admin");

        warehouse.setWarehouse_address(site);
        warehouse.setWarehouse_name(name);
        warehouse.setAdmin_realname(w_admin);

       int a=  wareservice.add_warehouse(warehouse);

        return a;
    }

    //修改用户之前的数据回显
    @RequestMapping(value = "/updeat_huixian",method= RequestMethod.POST)
    @ResponseBody
    public  Object updeat_huixian_warehous(HttpServletRequest request, HttpServletResponse response){
        Integer id=Integer.valueOf(request.getParameter("w_ID"));
        Warehouse warehouse=wareservice.updeat_huixian_warehous(id);

        return warehouse;
    }

    //修改仓库
    @RequestMapping(value="/update_ware",method= RequestMethod.POST)
    @ResponseBody
    public int update_warehouse(HttpServletRequest request){
        String name=request.getParameter("u_name");
        String site=request.getParameter("u_site");
        String w_admin=request.getParameter("u_admin");
        Integer id=Integer.valueOf(request.getParameter("w_ID"));

        warehouse.setWarehouse_id(id);
        warehouse.setWarehouse_address(site);
        warehouse.setWarehouse_name(name);
        warehouse.setAdmin_realname(w_admin);

        int a=  wareservice.update_warehouse(warehouse);
        System.out.println(a);

        return a;
    }

    //单项删除
    @RequestMapping(value="/delete_warehouse",method= RequestMethod.POST)
    @ResponseBody
    public Integer delete_warehouse(HttpServletRequest request){
        Integer id =Integer.valueOf(request.getParameter("id"));

        warehouse.setWarehouse_id(id);
        Integer a=wareservice.delete_warehouse(id);

        return a;
    }

    //批量删除
    @RequestMapping(value="/deleteWarehouse",method= RequestMethod.POST)
    @ResponseBody
    public Object deleteWarehouse(HttpServletRequest request){
        String plid=request.getParameter("ids");
        System.out.println(plid);
        String[] s1=plid.split(",");
        List<Integer> list=new ArrayList<Integer>();
        for (int i = 0; i < s1.length; i++) {
            list.add(Integer.valueOf(s1[i]));
        }

        Integer a=wareservice.deleteWarehouse(list);


        return a;
    }

}
